#include "syncClient.hpp"

namespace etcd {

#define CHECK_EXCEPTIONS(cmd) \
  try { \
    return cmd; \
  } catch (std::exception const & ex) { \
    return Response(500, ex.what()); \
  }

SyncClient::SyncClient(std::string const& address)
    : client(address) {
}

Response SyncClient::get(std::string const& key) {
    CHECK_EXCEPTIONS(client.get(key).get());
}

Response SyncClient::set(std::string const& key, std::string const& value, int ttl) {
    CHECK_EXCEPTIONS(client.set(key, value, ttl).get());
}

Response SyncClient::set(std::string const& key, std::string const& value, int64_t leaseId) {
    CHECK_EXCEPTIONS(client.set(key, value, leaseId).get());
}

Response SyncClient::add(std::string const& key, std::string const& value, int ttl) {
    CHECK_EXCEPTIONS(client.add(key, value, ttl).get());
}

Response SyncClient::add(std::string const& key, std::string const& value, int64_t leaseId) {
    CHECK_EXCEPTIONS(client.add(key, value, leaseId).get());
}

Response SyncClient::modify(std::string const& key, std::string const& value, int ttl) {
    CHECK_EXCEPTIONS(client.modify(key, value, ttl).get());
}

Response SyncClient::modify(std::string const& key, std::string const& value, int64_t leaseId) {
    CHECK_EXCEPTIONS(client.modify(key, value, leaseId).get());
}

Response SyncClient::modify_if(std::string const& key, std::string const& value, std::string const& old_value, int ttl) {
    CHECK_EXCEPTIONS(client.modify_if(key, value, old_value, ttl).get());
}

Response SyncClient::modify_if(std::string const& key, std::string const& value, std::string const& old_value, int64_t leaseId) {
    CHECK_EXCEPTIONS(client.modify_if(key, value, old_value, leaseId).get());
}

Response SyncClient::modify_if(std::string const& key, std::string const& value, int old_index, int ttl) {
    CHECK_EXCEPTIONS(client.modify_if(key, value, old_index, ttl).get());
}

Response SyncClient::modify_if(std::string const& key, std::string const& value, int old_index, int64_t leaseId) {
    CHECK_EXCEPTIONS(client.modify_if(key, value, old_index, leaseId).get());
}

Response SyncClient::rm(std::string const& key) {
    CHECK_EXCEPTIONS(client.rm(key).get());
}

Response SyncClient::rm_if(std::string const& key, std::string const& old_value) {
    CHECK_EXCEPTIONS(client.rm_if(key, old_value).get());
}

Response SyncClient::rm_if(std::string const& key, int old_index) {
    CHECK_EXCEPTIONS(client.rm_if(key, old_index).get());
}


Response SyncClient::rmdir(std::string const& key, bool recursive) {
    CHECK_EXCEPTIONS(client.rmdir(key, recursive).get());
}

Response SyncClient::ls(std::string const& key) {
    CHECK_EXCEPTIONS(client.ls(key).get());
}

Response SyncClient::leasegrant(int ttl) {
    CHECK_EXCEPTIONS(client.leasegrant(ttl).get());
}

// Response SyncClient::watch(std::string const & key, bool recursive)
// {
//   web::http::uri_builder uri("/v2/keys" + key);
//   uri.append_query("wait=true");
//   if (recursive)
//     uri.append_query("recursive=true");
//   return send_get_request(uri);
// }

// Response SyncClient::watch(std::string const & key, int fromIndex, bool recursive)
// {
//   web::http::uri_builder uri("/v2/keys" + key);
//   uri.append_query("wait=true");
//   uri.append_query("waitIndex", fromIndex);
//   if (recursive)
//     uri.append_query("recursive=true");
//   return send_get_request(uri);
// }

}